package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_zan;

import cn.com.api.service.ApiU_zanService;

import cn.com.util.*;


public class U_zanBaseController {

	private static Logger logger = Logger.getLogger(U_zanBaseController.class);
	
	@Autowired
	private ApiU_zanService apiU_zanService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "u_zan-list")
	public ModelAndView u_zan_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_zan_list.jsp");
		
		List<ApiU_zan> u_zans = apiU_zanService.findU_zans();
		mv.addObject("u_zans", u_zans);
		
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "u_zan-add-pre")
	public ModelAndView u_zan_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/u_zan_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiU_zan u_zan = apiU_zanService.findU_zanById(Integer.valueOf(id));
			mv.addObject("u_zan",u_zan);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "u_zan-add")
	public void u_zan_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		String conn_id = request.getParameter("conn_id");
		String user_id = request.getParameter("user_id");
		String zan_type = request.getParameter("zan_type");
		String add_time = request.getParameter("add_time");
		String is_cancel = request.getParameter("is_cancel");
		
		Map<String,Object> map = new HashMap<String,Object>();
		//String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiU_zan u_zan = new ApiU_zan();
            /*
		    u_zan.setId(id);
		    u_zan.setConn_id(conn_id);
		    u_zan.setUser_id(user_id);
		    u_zan.setZan_type(zan_type);
		    u_zan.setAdd_time(add_time);
		    u_zan.setIs_cancel(is_cancel);
	        */
			apiU_zanService.insertU_zan(u_zan);
		}
		//edit
		else{
			ApiU_zan u_zan = apiU_zanService.findU_zanById(Integer.valueOf(id));
	        /*
		    u_zan.setId(id);
		    u_zan.setConn_id(conn_id);
		    u_zan.setUser_id(user_id);
		    u_zan.setZan_type(zan_type);
		    u_zan.setAdd_time(add_time);
		    u_zan.setIs_cancel(is_cancel);
			*/
			apiU_zanService.updateU_zan(u_zan);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
        JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
	/**
	 *删除
	 * @return
	 */
	@RequestMapping(value = "u_zan-delete")
	public void u_zan_delete(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		
		//delete
		int ret_i =	apiU_zanService.deleteU_zan(Integer.valueOf(id));
		
		//
		if(ret_i > 0){
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
}



